import sys
import os
sys.path.append("..")

from CoreSim.Unit import *
from CoreSim.Event import *
from CoreSim.Simulator import *
from CoreSim.SchedulerHybrid import *

if __name__ == "__main__":
    
    numHosts = 4
    numLinks = 4
    numStaticLinks = 2
    numSwitches = 16
    link_sh_Bw = 10.0 * Gb
    link_ss_Bw = 10.0 * Gb
    
    # workloads
    load = 0.6
    randomSeed = 1
    FLOW_LAUNCH_END_TIME = 2
    trafficType = 'CDF'
    workloads = ['DCTCP', 'VL2']
    scheduler = ['Jelly', 'Fully', 'Detour', 'OvS']
    cdfId = 0
    schedId = 3
    cdfFileName = "cdf/%s_CDF.txt"%workloads[cdfId]
    flowsFileName = "%s_%sToRs_%sHosts_%sLinks_%sLoad_%sTime.txt"\
                    %(workloads[cdfId], numSwitches, numHosts, numLinks, load, FLOW_LAUNCH_END_TIME)

    conf_interval = 10.0e-1     # Periodicity get the traffic matrix (Seconds)
    adjust_delay = 10.0e-3      # Circuit switching time (Seconds)
    sample_enable = True
    sample_interval = 1.0e-4    # sample network throughput
    
    '''
    compile_cmd = "g++ ./Input/fct.cc -o ./Input/fct -std=c++11"
    os.system(compile_cmd)
    '''
    if True:
        # topology
        numHosts = 4
        numLinks = 4
        numStaticLinks = 2
        numSwitches = 16
        # workload
        load = 0.6
        FLOW_LAUNCH_END_TIME = 0.2
        workloads = ['DCTCP'] #['DCTCP', 'VL2']
        # scheduler
        scheduler = ['Detour', 'OvS']
        # reconfigration and sample
        conf_interval   = 1.0e-1     # Periodicity get the traffic matrix (Seconds)
        adjust_delay    = 10.0e-3     # Circuit switching time (Seconds)
        sample_enable   = True
        sample_interval = 1.0e-4      # sample network throughput

    iteration_a = 0
    iteration_b = 1
    for i in xrange(iteration_a, iteration_b):
        for workload in workloads:
            cdfFileName = "cdf/%s_CDF.txt"%workload
            flowsFileName = "%s_%sToRs_%sHosts_%sLinks_%sLoad_%sTime.txt"\
                    %(workload, numSwitches, numHosts, numLinks, load, FLOW_LAUNCH_END_TIME)

            if not os.path.isfile(inDir+flowsFileName):
                print 'generate workload'
                run_cmd = "./Input/fct numHosts " + str(numHosts) + " numLinks " + str(numLinks) \
                    + " numSwitches " + str(numSwitches) + " link_sh_Bw " + str(link_sh_Bw) \
                    + " link_ss_Bw " + str(link_ss_Bw) + " load " + str(load) + " randomSeed " \
                    + str(randomSeed) + " cdfFileName " + inDir + cdfFileName + " flowsFileName " \
                    + inDir + flowsFileName + " FLOW_LAUNCH_END_TIME " + str(FLOW_LAUNCH_END_TIME) 
                os.system(run_cmd)

            for sched in scheduler:
                # initial new simulator
                print 'scheduler: %s-------------------------'%sched
                sim = Simulator()
                # enable reconfiguration
                if sched != 'Jelly':
                    event = UpdateTopologyEvent(conf_interval) # reconfigure event
                    sim.eventQueue.push(event, event.time)
                
                if sample_enable:
                    sampleFileName = "%s_out.txt"%sched
                    sampleFile = open(sampleFileName, "w")
                    event = SampleEvent(sample_interval, sample_interval, sampleFile) # reconfigure event
                    sim.eventQueue.push(event, event.time)
                    sim.AssignSdnScheduler(Scheduler=SchedulerHybrid(numLinks, numStaticLinks, numHosts, numSwitches, link_ss_Bw, sched, k=2), \
                        args=(trafficType, flowsFileName))
                    sim.sched.InitialTopology()
                    sim.Run()
                    sim.sched.PrintFlows()
                    sampleFile.close()
